import Cookies from 'js-cookie';
import * as API from '@/api';
import util from '@/utils/util';

const user = {

    state: {
        userInfo: JSON.parse(util.getSessionStorage('dx_myself') || '{}')
    },

    mutations: {

        logout(state, vm) {
            Cookies.remove('dx_user');
            localStorage.clear();
        },

        setUserInfo: function(state, data) {
            state.userInfo = data;
            util.setSessionStorage('dx_myself', JSON.stringify(state.userInfo));
        }

    },

    actions: {
        
        login({ commit, state }, obj) {
            return new Promise((resolve, reject) => {
                API.login({
                    data: {
                        username: obj.arguments.username,
                        password: obj.arguments.password
                        //confirm_number: obj.confirmNumber
                    },
                    success: function(response) {
                        //console.log(response);
                        if (typeof(response.err_code) !== 'undefined') {
                            commit('setUserInfo', response);
                        }
                        resolve(response);
                    }
                });
            });
        },

        logout({ commit, state }, obj) {
            console.log('login');
        }
    },

    getters: {}

};

export default user;
